package com.memory.wechat.controller;

import com.alibaba.fastjson.JSON;
import com.memory.modules.business.domain.earn.AccountEarn;
import com.memory.modules.business.domain.sell.InvoiceRecord;
import com.memory.modules.business.domain.sell.Sell;
import com.memory.modules.business.domain.user.Account;
import com.memory.modules.business.service.sell.InvoiceRecordService;
import com.memory.modules.business.service.sell.SellService;
import com.memory.modules.business.service.user.AccountService;
import com.memory.wechat.domain.util.WxRequestUtil;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 个人中心
 */
@Controller
@RequestMapping("personal")
public class PersonalCenterController {
    @Resource
    private AccountService accountService;

    @Resource
    private SellService sellService;

    @Resource
    private InvoiceRecordService invoiceRecordService;
    /**
     * 进入个人中心
     * @param model
     * @return
     */
    @GetMapping("/center")
    public String personalCenter(Model model) {
        return "/personal/center";
    }

    /**
     * 根据code获取openid
     * @param code
     * @return
     */
    @GetMapping("/info")
    @ResponseBody
    public String personalInfo(String code) {
        try {
            return WxRequestUtil.getOpenIdByCode(code);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 进入留言反馈解密
     * @param model
     * @return
     */
    @GetMapping("/kf")
    public String personalKf(String openid,Model model) {
        return "/personal/kefu";
    }

    /**
     * 进入购买记录
     * @param openid
     * @param model
     * @return
     */
    @GetMapping("/buy/list")
    public String buyList(String openid,Model model) {
        Account account = accountService.findByOpenId(openid);
        Sell sell = new Sell();
        sell.setAccountId(account.getId());
        Example<Sell> example = Example.of(sell);
        List<Sell> list = sellService.findListNoPage(example);
        model.addAttribute("sells",list);
        model.addAttribute("openid",openid);
        return "/personal/buyrecords";
    }

    /**
     * 进入开票申请
     * @param openid
     * @param model
     * @return
     */
    @GetMapping("/buy/apply")
    public String buyApply(String openid,Long recordid,Model model) {
        model.addAttribute("recordid",recordid);
        model.addAttribute("openid",openid);
        return "/personal/buyapply";
    }

    /**
     * 提交开票申请
     * @param openid
     * @return
     */
    @GetMapping("/buy/apply/submit")
    @ResponseBody
    public String buyApplySubmit(@RequestParam("openid") String openid, Long recordid, String address, String postal,
                                 String invoice, String batch, String email, String phone) {
        Sell sell = sellService.findById(recordid).get();
        if(sell != null) {
            sell.setInvoice(2);
            sellService.update(recordid,sell);
            InvoiceRecord invoiceRecord = InvoiceRecord.builder()
                    .accountId(sell.getAccountId())
                    .title(invoice)
                    .invoiceNo(batch)
                    .email(email)
                    .phone(phone)
                    .postCode(postal)
                    .address(address)
                    .createDate(new Date())
                .build();
            invoiceRecordService.create(invoiceRecord);
        }
        return "/personal/buyapply";
    }
}
